package com.ruoyi.crop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.crop.api.domain.Crop;
import com.ruoyi.crop.api.domain.MaterialLot;
import com.ruoyi.crop.service.CropService;
import com.ruoyi.crop.service.MaterialLotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

@RestController
@RequestMapping("/material")
public class MaterialLotController extends BaseController {

    @Autowired
    private MaterialLotService materialLotService;

    @Autowired
    private CropService cropService;


    //批次列表
    @GetMapping("/list")
    public TableDataInfo list(MaterialLot materialLot){
        startPage();
        List<MaterialLot> list = materialLotService.listMaterialLot(materialLot);
        return getDataTable(list);
    }


    //批次列表添加
    @PostMapping
    public AjaxResult saveMaterialLot(@RequestBody MaterialLot materialLot){
        long l = System.currentTimeMillis();
        materialLot.setMaterialCode("PC"+l);
        materialLot.setCreateBy(SecurityUtils.getUsername());
        materialLot.setCreateTime(new Date());
        LambdaQueryWrapper<MaterialLot> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MaterialLot::getCropCode,materialLot.getCropCode());
        MaterialLot one = materialLotService.getOne(wrapper);
        System.err.println(one);
        if(one!=null){
            return error("此批次号已被使用");
        }
        boolean save = materialLotService.save(materialLot);
        return toAjax(save);
    }


    //批次列表修改
    @PutMapping
    public AjaxResult updateMaterialLot(@RequestBody MaterialLot materialLot){
        materialLot.setUpdateBy(SecurityUtils.getUsername());
        materialLot.setUpdateTime(new Date());
        boolean b = materialLotService.updateById(materialLot);
        return toAjax(b);
    }


    //批次列表删除
    @DeleteMapping("/del")
    public AjaxResult delMaterialLot(Integer ids[]){
        boolean b = materialLotService.removeBatchByIds(Arrays.asList(ids));
        return toAjax(b);
    }

    //根据id 获取 对象信息
    @RequestMapping("/getById")
    public AjaxResult getById(Integer materialLotId){
        MaterialLot mater = materialLotService.getById(materialLotId);
        return success(mater);
    }

    //导出
    @Log(title = "生成批次编号管理列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MaterialLot materialLot)
    {
        List<MaterialLot> list = materialLotService.listMaterialLot(materialLot);
        ExcelUtil<MaterialLot> util = new ExcelUtil<MaterialLot>(MaterialLot.class);
        util.exportExcel(response, list, "批次编号管理");
    }

    //农作物主表
    @RequestMapping("croplist")
    public AjaxResult croplist(){
        List<Crop> list = cropService.list();
        return success(list);
    }


}
